﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AAA.Forms.Components
{
    public partial class ButtonX : Button
    {
        private Image _imgNormalBackgroundImage;
        private Image _imgMouseOverImage;
        private Image _imgMouseDownImage;
        private string _strNormalBackgroundImage;
        private string _strMouseOverImage;
        private string _strMouseDownImage;
        private int _iOffsetX;

        private bool _isCutBoundary;

        public bool IsCutBoundary
        {
            get { return _isCutBoundary; }
            set { _isCutBoundary = value; }
        }

        public Image NormalBackgroundImage
        {
            get { return _imgNormalBackgroundImage; }
            set { _imgNormalBackgroundImage = value; }
        }

        public Image MouseOverImage
        {
            get { return _imgMouseOverImage; }
            set { _imgMouseOverImage = value; }
        }

        public Image MouseDownImage
        {
            get { return _imgMouseDownImage; }
            set { _imgMouseDownImage = value; }
        }

        public int OffsetX
        {
            get { return _iOffsetX; }
            set { _iOffsetX = value; }
        }

        public ButtonX()
        {
            MouseEnter += new EventHandler(MouseEnterEvent);
            MouseMove += new MouseEventHandler(MouseMoveEvent);
            MouseDown += new MouseEventHandler(MouseDownEvent);
            MouseLeave += new EventHandler(MouseLeaveEvent);

            OffsetX = 0;
            DoubleBuffered = true;
            IsCutBoundary = true;
            //FlatAppearance.BorderSize = 0;
            //border = BorderStyle.None;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if ((Visible == false) || (Text == ""))
                return;
            PaintTitle(e.Graphics);
        }

        protected void PaintTitle(Graphics g)
        {
            SizeF fontSize;
            int iStartX;
            int iStartY;

            fontSize = g.MeasureString(Text, Font);

            iStartX = (int)((Width - fontSize.Width) / 2); ;
            if (OffsetX != 0)
                iStartX = OffsetX;

            iStartY = (int)((Height - fontSize.Height) / 2);

            g.DrawString(Text, Font, new SolidBrush(ForeColor),
                         new Point(iStartX, iStartY));
        }


        void MouseLeaveEvent(object sender, EventArgs e)
        {
            if ((NormalBackgroundImage == null) && (_strNormalBackgroundImage != null))
                NormalBackgroundImage = Image.FromFile(_strNormalBackgroundImage);
            ChangeImage(NormalBackgroundImage);
        }

        void MouseDownEvent(object sender, MouseEventArgs e)
        {
            if ((MouseDownImage == null) && (_strMouseDownImage != null))
                MouseDownImage = Image.FromFile(_strMouseDownImage);
            ChangeImage(MouseDownImage);
        }

        void MouseMoveEvent(object sender, MouseEventArgs e)
        {
            if ((MouseOverImage == null) && (_strMouseOverImage != null))
                MouseOverImage = Image.FromFile(_strMouseOverImage);
            ChangeImage(MouseOverImage);
        }

        void MouseEnterEvent(object sender, EventArgs e)
        {
            if ((MouseOverImage == null) && (_strMouseOverImage != null))
                MouseOverImage = Image.FromFile(_strMouseOverImage);
            ChangeImage(MouseOverImage);
        }

        public void LoadImage(string strNormal, string strMouseOver, string strMouseDown)
        {
            try
            {
                NormalBackgroundImage = Image.FromFile(strNormal);
                _strNormalBackgroundImage = strNormal;
                if (IsCutBoundary)
                    Util.BitmapRegion.CreateControlRegion(this, NormalBackgroundImage);
                ChangeImage(NormalBackgroundImage);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }

            try
            {
                MouseOverImage = Image.FromFile(strMouseOver);
                _strMouseOverImage = strMouseOver;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }

            try
            {
                MouseDownImage = Image.FromFile(strMouseDown);
                _strMouseDownImage = strMouseDown;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
        }

        private void ChangeImage(Image image)
        {
            try
            {
                if (image != null)
                    Image = image;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
        }
    }
}
